
from typing import Any, Dict
from abc import ABC, abstractmethod

class BaseModel(ABC):
    """所有映射类的基类"""

    def __init__(self):
        """初始化基类"""
        pass

    @abstractmethod
    def to_dict(self) -> Dict[str, Any]:
        """将对象转换为字典"""
        pass

    @classmethod
    @abstractmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'BaseModel':
        """从字典创建对象"""
        pass

    def to_json(self) -> str:
        """将对象转换为JSON字符串"""
        import json
        return json.dumps(self.to_dict(), default=str)

    @classmethod
    def from_json(cls, json_str: str) -> 'BaseModel':
        """从JSON字符串创建对象"""
        import json
        data = json.loads(json_str)
        return cls.from_dict(data)
